package com.yzb.ftp;

import com.yzb.helper.FtpClientHelper;
import org.apache.commons.net.io.CopyStreamEvent;
import org.apache.commons.net.io.CopyStreamListener;

import java.util.Date;

/**
 * <p>ftp进度监控类.</p>
 *
 * @author huang yu zhi
 * @version 1.0.0
 * @date 2019-01-29 14:32
 */
public class DefaultCopyStreamListener implements CopyStreamListener {
    /**总长度.*/
    private long totalSize;
    /**开始传输时间.*/
    private Date startDate;
    /**每次报告的传输间隔.*/
    private long intervalBytes;
    /**上一次传输的字节百分比.*/
    private int lastPercent = 0;
    /**字节传输百分比的次数.*/
    private int times = 1;

    /**
     * 总长度构造函数.
     * @param totalSize 总长度
     */
    public DefaultCopyStreamListener(long totalSize) {
        this.totalSize = totalSize;
        this.startDate = new Date();
        this.intervalBytes = (long) FtpClientHelper.FTP_CONFIG.getIntervalProgress() * totalSize;
    }

    /**
     *
     * @param event 事件
     */
    @Override
    public void bytesTransferred(CopyStreamEvent event) {
        System.out.println("event");
    }

    /**
     *
     * @param totalBytesTransferred 目前总共传输的字节输
     * @param bytesTransferred 每次传输的字节数
     * @param streamSize 流大小
     */
    @Override
    public void bytesTransferred(long totalBytesTransferred, int bytesTransferred, long streamSize) {
        int percent = (int) (totalBytesTransferred * 100 / this.totalSize);
        if (percent >= FtpClientHelper.FTP_CONFIG.getIntervalProgress() * this.times) {
            this.times++;
            System.out.println(percent);
            if (percent > this.lastPercent) {
                System.out.println("可以通知了");
                this.lastPercent = percent;
            }
        }
        if (totalBytesTransferred == this.totalSize) {
            System.out.println(this.startDate);
            System.out.println(new Date());
            System.out.println(this.totalSize);
            System.out.println(this.intervalBytes);
        }
    }
}
